package com.example.sys.org.model;

import com.example.core.convert.model.ModelConvertor_ModelListToString;
import com.example.core.convert.ModelToFormPropertyMap;
import com.example.core.convert.model.ModelConvertor_ModelToString;
import com.example.sys.org.form.SysOrgUserForm;
import com.fasterxml.jackson.annotation.JsonIgnore;
import lombok.Getter;
import lombok.Setter;

import javax.persistence.*;
import java.util.ArrayList;
import java.util.List;

@Setter
@Getter
@Entity
@Table(name = "sys_org_user")
public class SysOrgUser extends SysOrgElement {

    @Column(name = "fd_user_name")
    private String fdUserName;

    @Column(name = "fd_pass_word")
    private String fdPassWord;

    @OneToOne(cascade = CascadeType.ALL)
    @JoinColumn(name = "fd_address_id", referencedColumnName = "fd_id")
    private SysOrgAddress fdAddress;

    @ManyToMany(fetch = FetchType.EAGER)
    @JoinTable(name = "sys_org_user_role", joinColumns = {@JoinColumn(name = "fd_user_id", referencedColumnName = "fd_id")}, inverseJoinColumns = {@JoinColumn(name = "fd_role_id", referencedColumnName = "fd_id")})
    private List<SysOrgRole> fdRoles = new ArrayList<>();

    @JsonIgnore
    @Override
    public ModelToFormPropertyMap getToFormPropertyMap() {
        ModelToFormPropertyMap modelToFormPropertyMap = super.getModelToFormPropertyMap();
        modelToFormPropertyMap = new ModelToFormPropertyMap();
        modelToFormPropertyMap.put("fdAddress.fdId", new ModelConvertor_ModelToString("fdAddressId"));
        modelToFormPropertyMap.put("fdAddress.fdName", new ModelConvertor_ModelToString("fdAddressName"));
        modelToFormPropertyMap.put("fdRoles", new ModelConvertor_ModelListToString("fdRolesId:fdRolesName", "fdId:fdName"));
        return modelToFormPropertyMap;
    }

    @JsonIgnore
    @Override
    public Class<?> getFormClass() {
        return SysOrgUserForm.class;
    }

    @JsonIgnore
    @Override
    public void recalculateFields() {
        super.recalculateFields();
        this.fdOrgType = SysOrgConstant.PERSON;
    }
}
